﻿using Microsoft.Phone.Controls;
using MijnMultiOSRSSApp.Common.Helpers;
using MijnMultiOSRSSApp.Common.Models;
using MijnMultiOSRSSApp.Common.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;

namespace MijnMultiOSRSSApp.WP8
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            DataContext = ViewModelLocator.Main;
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox1.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox1.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));
            
            // Reset selected item to null (no selection)
            FeedListBox1.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox2.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox2.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox2.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox3.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox3.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox3.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox4.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox4.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox4.SelectedItem = null;
        }
        
        // Handle selection changed on FeedListSelector
        private void FeedListBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox5.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox5.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox5.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox6_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox6.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox6.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox6.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox7_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox7.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox7.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox7.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox8_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox8.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox8.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox8.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox9_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox9.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox9.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox9.SelectedItem = null;
        }

        // Handle selection changed on FeedListSelector
        private void FeedListBox10_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (FeedListBox10.SelectedItem == null)
                return;

            // Set the selected item
            ViewModelLocator.Main.SelectedItem = FeedListBox10.SelectedItem as RSSItem;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsPage.xaml", UriKind.Relative));

            // Reset selected item to null (no selection)
            FeedListBox10.SelectedItem = null;
        }
        
        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (ViewModelLocator.Main.Items1 == null || ViewModelLocator.Main.Items1.Count == 0)
                ViewModelLocator.Main.LoadData();

            ReviewNotification.TriggerReviewNotification();
        }
    }
}